﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JX.Infrastructure.LoadBalancer
{
    /// <summary>
    /// 随机算法 负载平衡器
    /// </summary>
    public class RandomLoadBalancer : ILoadBalancer
    {
        private readonly Random _random = new Random();

        /// <summary>
        /// 根据随机算法，解析出需要的元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sequence"></param>
        /// <returns></returns>
        public T Resolve<T>(IEnumerable<T> sequence)
        {
            var items = sequence.ToList();
            var index = _random.Next(items.Count);
            return items[index];
        }
    }
}
